package com.android.myapplication.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;

import com.android.myapplication.R;
import com.android.myapplication.main.BaseActvity;
import com.android.myapplication.view.adapter.StudentAdapter;
import com.android.myapplication.view.adapter.StudentBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class ListActivity extends BaseActvity {
    private String[] data = {"Apple1", "Apple2", "Apple3", "Apple4", "Apple5", "Apple6",
            "Apple7", "Apple8", "Apple9", "Apple10", "Apple11", "Apple12", "Apple13", "Apple14",
            "Apple15", "Apple16", "Apple17", "Apple18", "Apple19", "Apple20"};
    ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_list);
        init();
        mListView = findViewById(R.id.list);

        Spinner spinner = findViewById(R.id.spinner_adapter_type);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                                ListActivity.this, R.layout.list_item_student, R.id.txt_name, data
                        );
                        mListView.setAdapter(adapter);
                        break;
                    case 1:
                        String[] theme = {"张三", "李四", "王五"};
                        String[] content = {"我是张三，你好", "我是李四，你好", "我是王五，你好"};
                        int[] imageViews = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
                        ArrayList lists = new ArrayList<>();
                        for (int i = 0; i < theme.length; i++) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("image", imageViews[i]);
                            map.put("theme", theme[i]);
                            map.put("content", content[i]);
                            lists.add(map);
                        }
                        SimpleAdapter simpleAdapter = new SimpleAdapter(ListActivity.this, lists, R.layout.list_item_simple
                                , new String[]{"image", "theme", "content"}
                                , new int[]{R.id.image1, R.id.text1, R.id.text2});
                        mListView.setAdapter(simpleAdapter);

                        break;
                    case 2:
                        List<StudentBean> studentBeanList = new ArrayList<>();
                        Random random = new Random();
                        for(int i = 0; i < 10; i ++){
                            studentBeanList.add(new StudentBean("张" + (i + 1), random.nextInt(1), random.nextInt(5) + 16, "http://img2.imgtn.bdimg.com/it/u=2934897295,342218263&fm=26&gp=0.jpg", "软171401"));
                        }
                        StudentAdapter studentAdapter = new StudentAdapter(ListActivity.this);
                        studentAdapter.setData(studentBeanList);
                        mListView.setAdapter(studentAdapter);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });



    }
}